import 'package:flutter/material.dart';
import '../../../app_views.dart';

/// 运势卡片组件 - Fortune card component
/// 
/// 显示今日运势信息的卡片
/// Card component for displaying today's fortune information
class FortuneCard extends StatelessWidget {
  final TodayInfo? todayInfo;
  final VoidCallback? onTap;

  const FortuneCard({
    Key? key,
    this.todayInfo,
    this.onTap,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
      decoration: BoxDecoration(
        gradient: LinearGradient(
          begin: Alignment.topLeft,
          end: Alignment.bottomRight,
          colors: [
            AppColors.primary.withOpacity(0.8),
            AppColors.secondary.withOpacity(0.6),
          ],
        ),
        borderRadius: BorderRadius.circular(16),
        boxShadow: [
          BoxShadow(
            color: AppColors.primary.withOpacity(0.3),
            blurRadius: 12,
            offset: const Offset(0, 4),
          ),
        ],
      ),
      child: Material(
        color: Colors.transparent,
        child: InkWell(
          onTap: onTap,
          borderRadius: BorderRadius.circular(16),
          child: Padding(
            padding: const EdgeInsets.all(20),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                // 标题行 - Title row
                Row(
                  children: [
                    Icon(
                      Icons.auto_awesome,
                      color: AppColors.textLight,
                      size: 24,
                    ),
                    const SizedBox(width: 8),
                    Text(
                      '今日运势',
                      style: GoogleFonts.notoSansSc(
                        fontSize: 18,
                        fontWeight: FontWeight.w600,
                        color: AppColors.textLight,
                      ),
                    ),
                    const Spacer(),
                    Icon(
                      Icons.arrow_forward_ios,
                      color: AppColors.textLight.withOpacity(0.7),
                      size: 16,
                    ),
                  ],
                ),
                const SizedBox(height: 16),
                
                if (todayInfo != null) ...[
                  // 日期信息 - Date information
                  _buildDateInfo(),
                  const SizedBox(height: 12),
                  
                  // 运势信息 - Fortune information
                  _buildFortuneInfo(),
                ] else ...[
                  // 加载状态 - Loading state
                  _buildLoadingState(),
                ],
              ],
            ),
          ),
        ),
      ),
    );
  }

  /// 构建日期信息 - Build date information
  Widget _buildDateInfo() {
    return Row(
      children: [
        Expanded(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                todayInfo!.solarDate,
                style: GoogleFonts.notoSansSc(
                  fontSize: 16,
                  fontWeight: FontWeight.w500,
                  color: AppColors.textLight,
                ),
              ),
              const SizedBox(height: 4),
              Text(
                '${todayInfo!.lunarDate} ${todayInfo!.weekday}',
                style: GoogleFonts.notoSansSc(
                  fontSize: 14,
                  color: AppColors.textLight.withOpacity(0.8),
                ),
              ),
            ],
          ),
        ),
        Container(
          padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
          decoration: BoxDecoration(
            color: AppColors.textLight.withOpacity(0.2),
            borderRadius: BorderRadius.circular(12),
          ),
          child: Text(
            todayInfo!.constellation,
            style: GoogleFonts.notoSansSc(
              fontSize: 12,
              fontWeight: FontWeight.w500,
              color: AppColors.textLight,
            ),
          ),
        ),
      ],
    );
  }

  /// 构建运势信息 - Build fortune information
  Widget _buildFortuneInfo() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        // 生肖和干支 - Chinese zodiac and Gan Zhi
        Row(
          children: [
            _buildInfoChip('生肖', todayInfo!.chineseZodiac),
            const SizedBox(width: 12),
            _buildInfoChip('干支', todayInfo!.ganZhi),
          ],
        ),
        const SizedBox(height: 12),
        
        // 五行和纳音 - Five elements and Nayin
        Row(
          children: [
            _buildInfoChip('五行', todayInfo!.fiveElements),
            const SizedBox(width: 12),
            _buildInfoChip('纳音', todayInfo!.nayin),
          ],
        ),
        
        // 节气信息 - Solar term information
        if (todayInfo!.hasSolarTerm) ...[
          const SizedBox(height: 12),
          Container(
            padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
            decoration: BoxDecoration(
              color: AppColors.accent.withOpacity(0.3),
              borderRadius: BorderRadius.circular(8),
              border: Border.all(
                color: AppColors.accent.withOpacity(0.5),
                width: 1,
              ),
            ),
            child: Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                Icon(
                  Icons.wb_sunny,
                  color: AppColors.textLight,
                  size: 16,
                ),
                const SizedBox(width: 6),
                Text(
                  '节气：${todayInfo!.solarTerm}',
                  style: GoogleFonts.notoSansSc(
                    fontSize: 12,
                    fontWeight: FontWeight.w500,
                    color: AppColors.textLight,
                  ),
                ),
              ],
            ),
          ),
        ],
        
        // 节日信息 - Festival information
        if (todayInfo!.hasFestivals) ...[
          const SizedBox(height: 12),
          Wrap(
            spacing: 8,
            runSpacing: 4,
            children: todayInfo!.allFestivals.map((festival) {
              return Container(
                padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
                decoration: BoxDecoration(
                  color: AppColors.error.withOpacity(0.3),
                  borderRadius: BorderRadius.circular(6),
                ),
                child: Text(
                  festival,
                  style: GoogleFonts.notoSansSc(
                    fontSize: 11,
                    fontWeight: FontWeight.w500,
                    color: AppColors.textLight,
                  ),
                ),
              );
            }).toList(),
          ),
        ],
      ],
    );
  }

  /// 构建信息芯片 - Build information chip
  Widget _buildInfoChip(String label, String value) {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 6),
      decoration: BoxDecoration(
        color: AppColors.textLight.withOpacity(0.15),
        borderRadius: BorderRadius.circular(8),
      ),
      child: RichText(
        text: TextSpan(
          children: [
            TextSpan(
              text: '$label: ',
              style: GoogleFonts.notoSansSc(
                fontSize: 12,
                color: AppColors.textLight.withOpacity(0.8),
              ),
            ),
            TextSpan(
              text: value,
              style: GoogleFonts.notoSansSc(
                fontSize: 12,
                fontWeight: FontWeight.w500,
                color: AppColors.textLight,
              ),
            ),
          ],
        ),
      ),
    );
  }

  /// 构建加载状态 - Build loading state
  Widget _buildLoadingState() {
    return Column(
      children: [
        Row(
          children: [
            Container(
              width: 120,
              height: 16,
              decoration: BoxDecoration(
                color: AppColors.textLight.withOpacity(0.3),
                borderRadius: BorderRadius.circular(8),
              ),
            ),
            const Spacer(),
            Container(
              width: 60,
              height: 16,
              decoration: BoxDecoration(
                color: AppColors.textLight.withOpacity(0.3),
                borderRadius: BorderRadius.circular(8),
              ),
            ),
          ],
        ),
        const SizedBox(height: 12),
        Row(
          children: [
            Container(
              width: 80,
              height: 14,
              decoration: BoxDecoration(
                color: AppColors.textLight.withOpacity(0.2),
                borderRadius: BorderRadius.circular(7),
              ),
            ),
            const SizedBox(width: 12),
            Container(
              width: 100,
              height: 14,
              decoration: BoxDecoration(
                color: AppColors.textLight.withOpacity(0.2),
                borderRadius: BorderRadius.circular(7),
              ),
            ),
          ],
        ),
      ],
    );
  }
}